var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listCategoryOne: [],
    categoryParentid: "",
    categorySelect: "",
    name: "",
    minPrice: "",
    maxPrice: "",
    showlist: "",
    categoryId:"",
    color:"",
    buthidden:true,
    yz1: true,
    yz2: true,
    yz3: true,
  },
  inputname: function (e) {
    var that = this;
    wx.setStorage({//存储到本地
      key: "x",
      data: e.detail.value
    })
    console.log(wx.getStorageSync('x') !== '', wx.getStorageSync('x') !== '' && wx.getStorageSync('y') !== '' && wx.getStorageSync('z') !== '')
    if (wx.getStorageSync('x') !== '' && wx.getStorageSync('y') !== '' && wx.getStorageSync('z') !== '') {

      that.setData({
        buthidden: false
      })
    }else{
      that.setData({
        buthidden: true
      })
    }
    
    that.setData({
      name: e.detail.value
    })
  },
    
  bindFormSubmit: function () {
    var user_cat = wx.getStorageSync('cateid');
    var user_cid = wx.getStorageSync('cid');
    var that = this;
    that.setData({
      categorySelect: user_cid
    })
    console.log(that.data.categorySelect)
    wx.request({
      method: "POST",
      url: app.preUrl +'updateSiteCategory',//修改类目
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded;',
      },
      data: {
        id:user_cat,
        userId: app.globalData.userId,
        parentId: that.data.categorySelect,
        //categoryId: that.data.categorySelect,
        name: that.data.name,
        minimumPrice: that.data.minPrice,
        highestPrice: that.data.maxPrice,
      },
      success: function (res) {
        console.log(res)
      },
      fail: function (error) {
        console.log("error", error);
      }
    })

    wx.navigateBack({
      delta: 1,

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var user_cat = wx.getStorageSync('cateid');
    console.log(user_cat)
    var wxid = wx.getStorageSync('cateid');
    console.log(wxid)
   

    var cid= wx.getStorageSync('cid');
    that.setData({
      categorySelect: cid
    })
    console.log(that.data.categorySelect)
    
    wx.request({
      method: "POST",
      url: app.preUrl +'getSiteCategoryId',//根据id查询
      header: {
        'content-type': 'application/x-www-form-urlencoded;',
      },
      data:{
        id: wxid
      },
      success: function (res) {
        console.log(res.data.result)
        that.setData({
          name: res.data.result.name,
          minPrice: res.data.result.minimumPrice,
          maxPrice: res.data.result.highestPrice,
          categoryId: res.data.result.categoryId
          // categorySelect: res.data.result.categoryId
        })
        console.log(that.data.name, that.data.minPrice, that.data.maxPrice)
        if (that.data.minPrice==undefined){
          wx.setStorage({//存储到本地
            key: "y",
            data: ""
          })
        }
        if (that.data.maxPrice == undefined) {
          wx.setStorage({//存储到本地
            key: "z",
            data: ""
          })
        }
        wx.setStorage({//存储到本地
          key: "x",
          data: that.data.name
        })
        wx.setStorage({//存储到本地
          key: "y",
          data: that.data.minPrice
        })
        wx.setStorage({//存储到本地
          key: "z",
          data: that.data.maxPrice
        })
        console.log(wx.getStorageSync('x') != '' && wx.getStorageSync('y') != '' && wx.getStorageSync('z') != '')

        if (wx.getStorageSync('x') != '' && wx.getStorageSync('y') != '' && wx.getStorageSync('z') != '') {
        
          that.setData({
            buthidden : false
          })
        }
        if (wx.getStorageSync('y') == undefined || wx.getStorageSync('z') == undefined) {
          
          that.setData({
            buthidden: true
          })
        }
      },
      fail: function (error) {
        console.log("error", error);

      }
    })

  },
  //一级菜单选择
  categoryList: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.select)
    wx.setStorage({//存储到本地

      key: "cid",
      data: e.currentTarget.dataset.select
    })
    that.setData({//把选中值放入判断值
      categorySelect: e.currentTarget.dataset.select
    });
  },

  //录入最低价
  onMinPrice: function (e) {
    var that = this;
    wx.setStorage({//存储到本地
      key: "y",
      data: e.detail.value
    })
    if (wx.getStorageSync('z') == undefined) {

      that.setData({
        buthidden: true
      })
    }
    else if (wx.getStorageSync('x') !== '' && wx.getStorageSync('y') !== '' && wx.getStorageSync('z') !== '') {

      that.setData({
        buthidden: false
      })
    } else {
      that.setData({
        buthidden: true
      })
    }
   
    that.setData({ minPrice: e.detail.value.replace(/[^\d.]/g, "") });
    that.setData({ minPrice: e.detail.value.replace(/\.{2,}/g, ".") });
    that.setData({ minPrice: e.detail.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".") });
    that.setData({ minPrice: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') });
  },

  //录入最高价
  onMaxPrice: function (e) {
    var that = this;
    wx.setStorage({//存储到本地
      key: "z",
      data: e.detail.value
    })
    if (wx.getStorageSync('y') == undefined) {

      that.setData({
        buthidden: true
      })
    }
    else if (wx.getStorageSync('x') !== '' && wx.getStorageSync('y') !== '' && wx.getStorageSync('z') !== '') {

      that.setData({
        buthidden: false
      })
    } else {
      that.setData({
        buthidden: true
      })
    }
    // if (e.detail.value == '') {
    //   that.setData({
    //     yz1: false,

    //   })
    // } else {
    //   that.setData({
    //     yz1: true,

    //   })
    // }

    // console.log(that.data.yz1 == true && that.data.yz2 == true && that.data.yz3 == true)
    // if (that.data.yz1 == true && that.data.yz2 == true && that.data.yz3 == true) {
    //   that.setData({
    //     buthidden: false,

    //   })

    // } else {
    //   that.setData({
    //     buthidden: true,

    //   })
    // }
    
    that.setData({ maxPrice: e.detail.value.replace(/[^\d.]/g, "") });
    that.setData({ maxPrice: e.detail.value.replace(/\.{2,}/g, ".") });
    that.setData({ maxPrice: e.detail.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".") });
    that.setData({ maxPrice: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      method: "POST",
      url: app.preUrl +'getSysCategoryList',//一级分类
      header: {
        'content-type': 'application/x-www-form-urlencoded;',
      },
      success: function (res) {
        that.setData({ listCategoryOne: res.data.result });
      },
      fail: function (error) {
        console.log("error", error);

      }
    })
    wx.request({
      method: "POST",
      url: app.preUrl +'getListCategory',//查询当前用户所有分类
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded;',
      },
      data: {
        userId: app.globalData.userId,
      },
      success: function (res) {
        console.log(res.data.result)
        that.setData({ listCategoryOne: res.data.result });
      },
      fail: function (error) {
        console.log("error", error);

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})